package Demo10;
import java.util.Scanner;
class Data {
    int need;
    String name;
    public Data(int need, String name) {
        this.need = need;
        this.name = name;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        //所需分配空间的进程数len
        int len = s.nextInt();
        int[] list = new int[64];
        list[2] = list[7] = list[13] = list[23] = list[37] = list[47] = list[59] = list[61] = 1;
        //len个进程每个所占用的内存数
        Data[] data = new Data[len];
        for (int i = 0; i < len; i++) {
            int num = s.nextInt();
            String name = "p" + (i + 1);
            data[i] = new Data(num, name);
        }
        //需要显示的进程名
        String thread_name = s.next();
        int[] index = new int[64];
        int count = 0;
        int lenth = list.length - 8;//当前仅剩的空闲区数量
        int sum = 0;
        int j = 0,k=0;
        for (int i = 0; i < data.length; i++) {
            sum += data[i].need;
            if (lenth - sum >= 0) {
                while (j < list.length) {
                    if (list[j] == 0) {
                        //
                        if(thread_name.equals(data[i].name)){
                            index[k++] = j;
                        }
                        count++;
                        if (count == data[i].need) {
                            System.out.print(j++ + " ");
                            count = 0;
                            break;
                        }
                    }
                    j++;
                }
            } else if (lenth - sum < 0 || data[i].need >= 64) {
                System.out.print("false ");
                sum -= data[i].need;
            }
        }
        System.out.print("\n");
        if(k==0) System.out.print("false ");
        for (int i = 0; i < k; i++) {
            System.out.print(index[i]+" ");
        }
    }
}
